<?php

/**
 * YZW与各个相关网站的API接口
 */
class YzwBaseAPI {

    //用户的信息
    public $userInfo = array();
    //需要发送的信息
    public $arrSendData = array();
    // 错误信息
    protected $error = '';
    //请求的链接
    protected $url = '';

    protected function __construct($userInfo = '') {
        $this->userInfo = empty($userInfo) ? Yii::app()->user->getInfo() : $userInfo;
    }

    /**
     * 单例
     * @staticvar null $handier
     * @return 
     */
    static public function getInstance($userInfo = '') {
        static $handier = NULL;
        if (empty($handier)) {
            $className = get_called_class();
            $handier = new $className($userInfo);
        }
        return $handier;
    }

    /**
     * 通过CURL请求接口
     * @return [type] [description]
     */
    public function sendCurl() {
        if(!$this->url){
            $this->error = '请求链接错误';
            return false;
        }
        $arrData = json_encode($this->arrSendData, true);
        $arrData = Encrypt::authcode($arrData, '', Yii::app()->params['bbsInfo']['key']); //加密
        $arrData = array('sign' => $arrData);

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $this->url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $arrData);
        curl_setopt($ch, CURLOPT_NOBODY, 0);
        $output = curl_exec($ch);
        curl_close($ch);
        return $output = json_decode($output, true);
    }

    /**
     * 取得最后一次错误信息
     * @access public
     * @return string
     */
    public function getError() {
        return $this->error;
    }

}
